package com.hc.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hc.domain.User;
import com.hc.service.UserService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;

public class MyUserDetailsService implements UserDetailsService {
    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("MyUserDetailsService- loadUserByUsername:"+username);
        User user = userService.getOne(new QueryWrapper<User>().select("password").eq("username", username));
        if (user == null) {
            throw new UsernameNotFoundException("指定名称的用户不存在");
        }
        org.springframework.security.core.userdetails.User res = new org.springframework.security.core.userdetails.User(username, user.getPassword(), AuthorityUtils.createAuthorityList());
        return res;
    }
}
